package com.chaowen.util;

import android.content.Context;
import android.net.wifi.WifiManager;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Toast;

import com.chaowen.config.Config;
import com.chaowen.yixin.Fragment_answer;
import com.chaowen.yixin.Fragment_login;
import com.chaowen.yixin.Fragment_sign;
import com.chaowen.yixin.MainActivity;
import com.chaowen.yixin.R;
import com.chaowen.yixin.RightSlidingMenuFragment;

public class MyHandler extends Handler{
	private MainActivity mainActivity;
	public MyHandler(MainActivity mainActivity){
		this.mainActivity = mainActivity;
	}
	
	@Override
	public void handleMessage(Message msg) {
		super.handleMessage(msg);
		int what = msg.what;
		int i = -1;
		Object localObject = null;
		switch (what) {
		case 0:
			localObject = "用户名或者密码为空";
			break;
//		case 1:
//			localObject = "服务器断开连接";
//			break;
		case -1:
			localObject = "服务器未开启数据库服务";
			break;
		case 2:
			localObject = "请先连接到当前课堂wifi";
			break;
		case 10:
			localObject = "登陆失败,用户名或者密码错误";
			break;
		case 11:
			i = 0;
			localObject = "登陆成功";
			break;
		case 12:
			i = -1;
			localObject = "退出成功";
			break;
		case 3:
			localObject = "签到出现错误,稍后重试";
			i = 3;
			break;
		case 20:
//			localObject = "已经完成签到";
			i = 1;
			break;
		case 21:
			localObject = "签到成功,请开始答题";
			i = 1;
			//签到完成以后，接受答题广播
			WifiManager manager = (WifiManager) mainActivity
					.getSystemService(Context.WIFI_SERVICE);
			Thread thread = new Thread(new AnswerClient(manager, this));
			thread.start();
			break;
		case 22:
			localObject = "对不起，已关闭签到";
			i = 3;
			break;
		case 4:
			localObject = "答题出现错误";
			i = 4;
			break;
		case 28:
//			localObject = "请开始答题";
			Config.isAnswer = true;
			i = 2;
			break;
//		case 29:
//			localObject = "请等待开始答题命令";
//			i = 4;
//			break;
		case 30:
			localObject = "这道题您已经答过了";
			i = 2;
			break;
		case 31:
//			localObject = "答题成功";
			i = 2;
			break;
		case 32:
			localObject = "已关闭答题";
			i = 4;
			break;
		case 33:
			localObject = "选项不能为空";
			i = 4;
			break;	
		case 100:
			RightSlidingMenuFragment.notifyDataSetChanged();
//			localObject = "正在扫描...";
			i = 5;
			break;
		case 101:
//			localObject = "扫描结束";
			i = 6;
			break;	
		case 102:
			localObject = "未找到可用wifi";
			i = 6;
			break;
		case 103:
			localObject = "开始扫描...";
			i = 5;
			break;
		}
		if (localObject != null)
			Toast.makeText(mainActivity, localObject + "",
					Toast.LENGTH_SHORT).show();
		// 登陆成功
		if (i == 0) {
			Fragment_login.loginBtn.setText("退出");
			Fragment_login.username.setEnabled(false);
			Fragment_login.password.setEnabled(false);
			Fragment_login.rememberTextView.setEnabled(false);
			RightSlidingMenuFragment.listView.setEnabled(false);
			RightSlidingMenuFragment.openWifi.setEnabled(false);
		}
		// 登陆失败
		else if (i == -1) {
			Fragment_login.loginBtn.setText("登陆");
			Fragment_login.loginBtn.setEnabled(true);
			Fragment_login.username.setEnabled(true);
			Fragment_login.password.setEnabled(true);
			Fragment_login.rememberTextView.setEnabled(true);
			RightSlidingMenuFragment.listView.setEnabled(true);
			RightSlidingMenuFragment.openWifi.setEnabled(true);
		} 
		// 签到成功
		else if (i == 1) {
			Fragment_sign.signBtn.setEnabled(false);
			Fragment_sign.signBtn.setText("签到成功");
			Fragment_sign.signBtn.setBackgroundResource(R.drawable.press_button);
			Calculate.list.clear();
			Calculate.dateList.clear();
			RightSlidingMenuFragment.notifyDataSetChanged();
		}
		// 签到失败
		else if (i == 3) {
			Fragment_sign.signBtn.setText("签到");
			Fragment_sign.signBtn.setEnabled(true);
			Calculate.list.clear();
			Calculate.dateList.clear();
			RightSlidingMenuFragment.notifyDataSetChanged();
		}
		// 答题成功 
		else if(i==2) {
			if(Fragment_answer.answerBtn==null) return;
			if(Config.startNumbers == 0) return;
			if(Fragment_answer.radioGroup!=null) Fragment_answer.radioGroup.check(-1);
			Fragment_answer.answerBtn.setVisibility(View.VISIBLE);
			Fragment_answer.answerBtn.setText("提交答案");
			if(Config.startNumbers<=Config.endNumbers) {
				if(Config.startNumbers>=Config.singleStart&&Config.startNumbers<=Config.singleEnd) {
					Fragment_answer.answerNmuber.setText(Config.paperName+"\r\n 单项选择题    题号："+Config.startNumbers);
					Fragment_answer.radioGroup.setVisibility(View.VISIBLE);
					Fragment_answer.multipleGroup.setVisibility(View.INVISIBLE);
					Fragment_answer.digitalGroup.setVisibility(View.INVISIBLE);
				}else if(Config.startNumbers>=Config.multipleStart&&Config.startNumbers<=Config.multipleEnd) {
					Fragment_answer.answerNmuber.setText(Config.paperName+"\r\n 多项选择题    题号："+Config.startNumbers);
					Fragment_answer.multipleGroup.setVisibility(View.VISIBLE);
					Fragment_answer.radioGroup.setVisibility(View.INVISIBLE);
					Fragment_answer.digitalGroup.setVisibility(View.INVISIBLE);
				}else if(Config.startNumbers>=Config.digitalStart&&Config.startNumbers<=Config.digitalEnd) {
					Fragment_answer.answerNmuber.setText(Config.paperName+"\r\n 数字题    题号："+Config.startNumbers);
					Fragment_answer.digitalGroup.setVisibility(View.VISIBLE);
					Fragment_answer.multipleGroup.setVisibility(View.INVISIBLE);
					Fragment_answer.radioGroup.setVisibility(View.INVISIBLE);
				}
			}else {
				Fragment_answer.answerNmuber.setText("您已经答完所有题目");
				Fragment_answer.answerBtn.setVisibility(View.INVISIBLE);
				Fragment_answer.digitalGroup.setVisibility(View.INVISIBLE);
				Fragment_answer.multipleGroup.setVisibility(View.INVISIBLE);
				Fragment_answer.radioGroup.setVisibility(View.INVISIBLE);
			}
		}
		//答题失败
		else if(i == 4) {
			Fragment_answer.answerBtn.setText("提交答案");
		}
		else if (i == 5){
//			startWifi.setText("正在扫描...");
//			startWifi.setEnabled(false);
		}
		else if(i == 6) {
//			startWifi.setText("扫描可用wifi");
//			startWifi.setEnabled(true);
			RightSlidingMenuFragment.openWifi.setChecked(false);
		}
	}
	
}